/*
 * To change this template, choose Tools | Templates and open the template in
 * the editor.
 */
package goodenglish.infrastructure;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.sun.speech.freetts.Voice;
import com.sun.speech.freetts.VoiceManager;

/**
 *
 * @author 关攀攀 panpan_001@126.com
 */
public class EnTTS {

    private static Log logger = LogFactory.getLog(EnTTS.class);
    /**
     * alan (time domain) kevin (general domain) kevin16 (general domain)
     */
    private static String voiceName = "kevin16";
    private static VoiceManager voiceManager = VoiceManager.getInstance();
    private static Voice helloVoice = voiceManager.getVoice(voiceName);

    public static void main(String[] args) {
        speak("hello");
    }

    /**
     * Example of how to list all the known voices.
     */
    public static void listAllVoices() {
        System.out.println();
        System.out.println("All voices available:");
        VoiceManager voiceManager = VoiceManager.getInstance();
        Voice[] voices = voiceManager.getVoices();
        for (int i = 0; i < voices.length; i++) {
            System.out.println("    " + voices[i].getName() + " (" + voices[i].getDomain() + " domain)");
        }
    }

    public static void speak(String string) {
        if (helloVoice == null) {
            logger.error("Cannot find a voice named " + voiceName + ".  Please specify a different voice.");
        }

        /*
         * Allocates the resources for the voice.
         */
        helloVoice.allocate();

        /*
         * Synthesize speech.
         */
        helloVoice.speak(string);

        /*
         * Clean up and leave.
         */
        //   helloVoice.deallocate();
    }
}
